﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.company
{
    public partial class index_company_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.index_company().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.index_company bll = new BLL.index_company();
            Model.index_company model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            txtImgUrl.Text = model.img_url;
            txtSortId.Text = model.sort_id.ToString();
            txtCompanyName.Text = model.company_title;
            lbCompanyName.Text = model.company_title;
            txtCompanyID.Text = model.company_id.ToString();

            lbQuanziName.Text = new BLL.quanzi().GetTitle(model.quanzi_id);
            txtQuanziID.Text = model.quanzi_id.ToString();
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.index_company model = new Model.index_company();
            BLL.index_company bll = new BLL.index_company();

            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.company_id = Utils.StrToInt(txtCompanyID.Text.Trim(), 0);
            model.company_title = txtCompanyName.Text.Trim();
            model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);
           
            if (bll.Add(model) > 0)
            {
                return true;
            }

            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.index_company bll = new BLL.index_company();
            Model.index_company model = bll.GetModel(_id);

            model.title = txtTitle.Text.Trim();
            model.img_url = txtImgUrl.Text.Trim();
            model.sort_id = Utils.StrToInt(txtSortId.Text.Trim(), 99);
            model.company_id = Utils.StrToInt(txtCompanyID.Text.Trim(), 0);
            model.company_title = txtCompanyName.Text.Trim();
            model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);

            if (bll.Update(model))
            {
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改企业信息成功！", "index_company_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                //ChkAdminLevel("sys_site_manage", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加企业信息成功！", "index_company_list.aspx", "parent.loadMenuTree");
            }
        }
    }
}